<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>轻量搜索 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_lite.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_lite.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_search_lite.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_search_lite.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="intro.html">你知道的, 为了搜索…​</a></span>
»
<span class="breadcrumb-node">轻量搜索</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_retrieving_a_document.html">« 检索文档</a>
</span>
<span class="next">
<a href="_search_with_query_dsl.html">使用查询领域专属语言搜索 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_search_lite"></a>轻量搜索<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/010_Intro/30_Tutorial_Search.asciidoc">edit</a>
</h2>
</div></div></div>
<p>一个 <code class="literal">GET</code> 是相当简单的，可以直接得到指定的文档。 现在尝试一下稍微高级点的功能，比如一个简单的搜索！</p>
<p>第一个尝试的几乎是最简单的搜索了。我们使用下列请求来搜索所有雇员：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/30_Simple_search.json"></div>
<p>可以看到，我们仍然使用索引库 <code class="word">megacorp</code> 以及类型 <code class="word">employee</code>，但不再是指定一个文档 ID，这次使用<code class="literal">_search</code>终端。返回结果包括了所有三个文档，放在数组 <code class="literal">hits</code> 中。一个搜索默认返回十条结果。</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
   "took":      6,
   "timed_out": false,
   "_shards": { ... },
   "hits": {
      "total":      3,
      "max_score":  1,
      "hits": [
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "3",
            "_score":         1,
            "_source": {
               "first_name":  "Douglas",
               "last_name":   "Fir",
               "age":         35,
               "about":       "I like to build cabinets",
               "interests": [ "forestry" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "1",
            "_score":         1,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "2",
            "_score":         1,
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}</pre>
</div>

<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>返回结果不仅告诉我们匹配了哪些文档，还包含了整个文档本身：给用户显示搜索结果所需的全部信息。</p>
</div>
</div>

<p>接下来，尝试下搜索姓氏为 "Smith" 的雇员。为此，我们将使用一个 <em>高亮</em> 搜索方法，这很容易通过命令行完成。这个方法一般涉及到一个 <em>查询字符串 (query-string)</em> 搜索，因为我们将搜索作为一个 URL 查询字符串参数来传递：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search?q=last_name:Smith</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/30_Simple_search.json"></div>
<p>我们仍然在请求路径中使用 <code class="literal">_search</code> 终端，并将查询本身赋值给参数 <code class="literal">q=</code> 。返回结果给出了所有的 Smith：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
   ...
   "hits": {
      "total":      2,
      "max_score":  0.30685282,
      "hits": [
         {
            ...
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="_retrieving_a_document.html">« 检索文档</a>
</span>
<span class="next">
<a href="_search_with_query_dsl.html">使用查询领域专属语言搜索 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>